<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .active {
            background-color: gold;
        }
    </style>
</head>

<body>
    <ul class="list" id="list">
        <!-- 需求：当鼠标移动到某一项的时候，当前项高亮为金色，其他项恢复原装 -->
        <li class="active">首页</li>
        <li>新闻</li>
        <li>关于</li>
    </ul>
</body>
<script>
    /* 使用到 排它的实现
    1. 先找到所有的li
    2. 给所有的li绑定鼠标移入事件
    3. 先把所有li的active移除
    4. 给当前的元素添加 active 类名
    */
    var oLis = document.getElementById('list').getElementsByTagName('li');
    console.log(oLis);

    /* for 循环里面把原先的var声明改为 let 声明 */
    for (let i = 0; i < oLis.length; i++) {

        oLis[i].onmouseover = function () {
            for (var j = 0; j < oLis.length; j++) {
                oLis[j].className = '';
            }
            oLis[i].className = 'active';
        }
    }

    // 底层代码逻辑：
    /*  {
         let i = 0;
         oLis[i].onmouseover = function () {
             for (var j = 0; j < oLis.length; j++) {
                 oLis[j].className = '';
             }
             oLis[i].className = 'active';
         }
     }
     {
         let i = 1;
         oLis[i].onmouseover = function () {
             for (var j = 0; j < oLis.length; j++) {
                 oLis[j].className = '';
             }
             oLis[i].className = 'active';
         }
     }
     {
         let i = 2;
         oLis[i].onmouseover = function () {
             for (var j = 0; j < oLis.length; j++) {
                 oLis[j].className = '';
             }
             oLis[i].className = 'active';
         }
 
     } */

</script>

</html>